package xcmg.device.web.basic;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.PurchaseDictionaryDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.StringUtil;
import xcmg.device.service.basic.PurchaseDictionaryService;
import xcmg.device.service.vo.basic.PurchaseDictionaryVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.Arrays;
import java.util.List;


/**
 * 采购方式字典操作
 * @Author huhuawei
 */
@RequestMapping("/purchaseDictionary")
@RestController
public class PurchaseDictionaryController extends BasicController{
    @Autowired
    private PurchaseDictionaryService purchaseDictionaryService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "查询采购方式（多个）")
    public ActionResult list(@RequestBody PurchaseDictionaryVO purchaseDictionaryVO) throws Exception{
        //如果是一级节点 orgId为0
        if(purchaseDictionaryVO.getpId() != null && "0".equals(purchaseDictionaryVO.getpId())){
            purchaseDictionaryVO.setOrgId("0");
        }else if(StringUtil.isEmpty(purchaseDictionaryVO.getOrgId())){
            purchaseDictionaryVO.setOrgId(getOrgId());
        }
        List<PurchaseDictionaryDO> list=purchaseDictionaryService.list(purchaseDictionaryVO);
        return actionValueResult(list);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "添加采购方式")
    public ActionResult add(@RequestBody PurchaseDictionaryVO model) throws Exception{

        model.setCreateId(getUserId());
        //更新人默认的创建人
        model.setUpdateId(getUserId());
        model.setOrgId(getOrgId());
        ErrorCode ret=purchaseDictionaryService.add(model);
        return actionResult(ret);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "更新采购方式")
    public ActionResult update(@RequestBody PurchaseDictionaryVO model)throws Exception{
        if(model==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        //更新创建人信息
        model.setUpdateId(getUserId());
        model.setOrgId(getOrgId());
        ErrorCode ret=purchaseDictionaryService.update(model);
        return actionResult(ret);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody PurchaseDictionaryVO model){
        String[] ids = model.getIds();
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=purchaseDictionaryService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        PurchaseDictionaryVO result=purchaseDictionaryService.item(id);
        return actionValueResult(result);
    }

}
